<?php
/**
 *      [迷你云] (C)2009-2012 南京恒为网络科技.
 *   软件仅供研究与学习使用，如需商用，请访问www.miniyun.cn获得授权
 * 
 */
?>
<?php

class AdminAppController extends MiniyunController
{
    public $layout='//layouts/admin';
    public function actionIndex()
    {
        $model= new AppListForm();
        if(Yii::app()->request->isPostRequest){
            $model->attributes=$_POST['AppListForm'];
            if($model->mutiAction()){                return;
            }
        }else{
            if(isset($_GET['AppListForm'])){
                $model->attributes=$_GET['AppListForm'];            }
        }
        $app = new ServerRegistry();
        $this->render('appList',array(
            'model'=>$model,
            'count'=>$app->count("osr_usa_id_ref != -1"),            'adminCount'=>$app->enabledCount(),            'disabledCount'=>$app->disabledCount(),        ));
    }
    
    public function actionDelete()
    {
        $model = new AppDeleteForm();
        $model->data = $model->search()->getData();
        if(Yii::app()->request->isPostRequest){
            $deleteCount = $model->deleteData();            $message = "删除  ".$deleteCount." 条app成功";
            $this->redirect(Yii::app()->createUrl("adminApp/index&message=".$message));
        }else{
            $this->render('appDelete',array(
             "data"=>$model->data
            ));
        }
    }
    
    public function actionDisabled()
    {
        $model = new AppDisabledForm();
        $model->data = $model->search()->getData();
        if(Yii::app()->request->isPostRequest){
            $disabledCount = $model->disabledData();            $message = "禁用  ".$disabledCount." 条app成功";
            $this->redirect(Yii::app()->createUrl("adminApp/index&message=".$message));
        }else{
            $this->render('appDisabled',array(
             "data"=>$model->data
            ));
        }
    }
    
    
    public function actionEnabled()
    {
        $model = new AppEnabledForm();
        $model->data = $model->search()->getData();
        if(Yii::app()->request->isPostRequest){
            $enabledCount = $model->enabledData();            $message = "解冻  ".$enabledCount." 条用户成功";
            $this->redirect(Yii::app()->createUrl("adminApp/index&message=".$message));
        }else{
            $this->render('appEnabled',array(
             "data"=>$model->data
            ));
        }
    }
    
    
    public function actionChangeRole()
    {
        $model = new ChangeRoleForm();
        $model->data = $model->search()->getData();
        $aim = Yii::app()->request->getParam("aim");
        if(Yii::app()->request->isPostRequest){
            if($aim=="admin"){
                $count = $model->changeRole(true);                $message = "改变  ".$count." 个用户为管理员";
                $this->redirect(Yii::app()->createUrl("adminUserPanel/index&message=".$message));
            }else{
                $count = $model->changeRole(false);                if($count==0)                {
                    $message = "未选中有效记录";
                }else{
                    $message = "改变  ".$count." 个用户为普通用户";
                }
                $this->redirect(Yii::app()->createUrl("adminUserPanel/index&message=".$message));
            }

        }else{
            $no = false;
            if($aim!="admin" && $model->isLastAdmin()){
                                $no = true;
            }
            $this->render('changeRole',array(
             "data"=>$model->data,
            "no"=>$no,
            ));
        }
    }
    
    public function actionStat(){
        $statForm = new UserStatForm();
        $statForm->model = new User();
        if(Yii::app()->request->isPostRequest){
            $statForm->startDate = $_POST['UserStatForm']["startDate"];
            $statForm->endDate   = $_POST['UserStatForm']["endDate"];
            $statForm->initData();
        }else{
            $statForm->initData();        }
        $this->render('stat',array(
             "data"=>$statForm,
        ));
    }



    
    public function actionDeleteDevice()
    {
        if (!empty($_GET['id']) && !empty($_GET['action']))
        {
            $device = new UserDevice();
                        $message = "删除设备成功";
            $this->redirect(Yii::app()->createUrl($this->id.'/'.$_GET['action'].'&message='.$message));
        }
    }

    
    public function actionCreate()
    {
        $model = new AppCreateForm();
        if(isset($_POST['AppCreateForm']))
        {
            $model->attributes=$_POST['AppCreateForm'];
            if($model->save()){
                $message = "新建APP成功";
                $this->redirect(Yii::app()->createUrl("adminApp/index&message=".$message));
            }
        }
        $this->render('appCreate',array(
                    'model'=>$model
        ));
    }

    
    public function actionUpdate()
    {
        $id=$_REQUEST['id'];
        $model = new AppUpdateForm();
        $model->initData($id);
        
        $test=isset($_POST['AppUpdateForm']);
        
        if(isset($_POST['AppUpdateForm']))
        {
            $model->attributes=$_POST['AppUpdateForm'];
            if($model->save()){
                $message = "修改APP成功";
                $this->redirect(Yii::app()->createUrl("adminApp/index&message=".$message));
            }
        } 
        $this->render('appUpdate',array(
            'model'         => $model,
        ));

    }
    
    public function actionShow()
    {
        if(empty($_REQUEST['id'])){
            $id=Yii::app()->session["user"]["id"];
        }else{
            $id=$_REQUEST['id'];
        }
        $model = new UserShowForm($id);
        if(Yii::app()->request->isPostRequest)
        { 
            $model->attributes=$_POST['UserShowForm'];
        }
        $model->initStatData();
        $this->render('userShow',array(
            'model'         => $model,
        ));

    }
}